<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-4" @click="router.go(-1)"> 返回 </a-button>
        <a-descriptions class="mt-3" :column="4">
          <a-descriptions-item label="检验大类"> {{ formState?.categroyName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="仪器类型">{{ formState?.instrumentTypeName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="备注"> {{ formState?.cmemo || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2 flex-1">
      <div class="">
        <a-tabs v-model:active-key="active">
          <a-tab-pane :key="1" tab="HIS" />
          <a-tab-pane :key="2" tab="体检" />
        </a-tabs>
      </div>
      <BaseTable
        row-key="id"
        :data-source="dataSource"
        column-code="000488"
        :loading="loading"
        max-fill
        :extra-height="64"
        :row-class-name="(record) => (record.isNew ? 'table-row-new' : '')"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'mc'">
            <EditCell title="药品名称">
              <SelectMedicine :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" type="对照项目" @select="onSelectMedicine($event, record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <BaseButton confirm-text="确认删除？" type="text" danger size="small" @click="onDelete(record)">删除</BaseButton>
          </template>
        </template>
        <template #footer>
          <div class="text-center sticky bottom-0">
            <a-button class="mr-2" :disabled="!formState?.categroyCode" type="primary" ghost @click="addRow()"> 新增一行 </a-button>
            <a-button type="primary" :disabled="dataSource.length === 0" @click="saveNewRows()"> 保存 </a-button>
          </div>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import EditCell from '@/components/EditCell'

const formState = ref({})
const active = ref(1)
const loading = ref(false)
const dataSource = ref([])
const route = useRoute()
const router = useRouter()
const saveNewRows = async (row) => {
  if (row?.isNew) return
  const newData = dataSource.value.filter((i) => i.mc)
  const { code, msg } = await post('/lis/InstrumentType/saveLisCategroyItemList', {
    categroyCode: route.query?.id,
    list: newData
  })
  if (code !== 0) return message.error(msg || '操作失败')
  getDetailData()
  message.destroy()
  message.success('保存成功')
}

const onSelectMedicine = async (medicine, row) => {
  row.dj = medicine?.dj
  row.mc = medicine?.mc
  row.lbmc = medicine?.lbmc
  row.gg = medicine?.gg
  row.cj = medicine?.cj
  row.orderItemCode = medicine?.bh
  await saveNewRows(row)
}
//添加行
const addRow = () => {
  const key = `row-${dataSource.value.length}`
  const addRow = { key, isNew: true }
  dataSource.value.push(addRow)
}

const getDetailData = async () => {
  loading.value = true
  const { data, code } = await post('/lis/InstrumentType/getLisCategroyItemList', {
    categroyCode: route.query?.id
  })
  loading.value = false
  if (code === 0) {
    dataSource.value = data
  }
}
const onDelete = async (row) => {
  if (row.isNew) {
    dataSource.value = dataSource.value.filter((item) => item.key !== row.key)
  } else {
    const { code, msg } = await post('/lis/InstrumentType/delLisCategroyItem', row)
    if (code !== 0) return message.error(msg || '删除失败')
    message.success(msg)
    getDetailData()
  }
}

// 获取信息
const getInstrumentType = async () => {
  const { code, data, msg } = await post('/lis/InstrumentType/getLisCategroy', { categroyCode: route.query?.id })
  if (code !== 0) return message.error(msg)
  formState.value = data
}

watchEffect(() => {
  if (route && route.query?.id) {
    getDetailData()
    getInstrumentType()
  }
})
</script>
<style lang="less" scoped>
:deep(.ant-card-head) {
  padding-left: 8px;
}
</style>
